﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class ConfigDAO:IData<Config> 
 	{
		public ConfigDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref Config item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + Config.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_Config", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Config.c_name, item.name), new SqlParameter("@" + Config.c_value, item.value),
				 new SqlParameter("@" + Config.c_note, item.note));
			if(t == 0)
				item.id = Convert.ToInt32(obj);
			return i;
		}

		public override Config GetItem(IDataReader dr)
		{
			Config obj = new Config();
			obj.id = Convert.ToInt32(dr[Config.c_id]);
			obj.name = Convert.ToString(dr[Config.c_name]);
			obj.value = Convert.ToString(dr[Config.c_value]);
			obj.note = Convert.ToString(dr[Config.c_note]);
			return obj;
		}

		public override List<Config> GetAll()
		{
			return GetAll("sp_GetAllConfig", CommandType.StoredProcedure);
		}

		public override Config GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt32(id));
		}
		public override Config GetItemById(Config item){
			throw new NotImplementedException();
		}

	}

 }
